#!/bin/sh
################################################################################
# Xinc - Continuous Integration for PHP.                                         
# 
# package Xinc
# author David Ellis
# author Gavin Foster
# version 1.0
# copyright 2007 David Ellis, One Degree Square
# license  http://www.gnu.org/copyleft/lgpl.html GNU/LGPL, see license.php
# 	This file is part of Xinc.
# 	Xinc is free software; you can redistribute it and/or modify
# 	it under the terms of the GNU Lesser General Public License as published by
# 	the Free Software Foundation; either version 2.1 of the License, or
# 	(at your option) any later version.
# 
# 	Xinc is distributed in the hope that it will be useful,
# 	but WITHOUT ANY WARRANTY; without even the implied warranty of
# 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# 	GNU Lesser General Public License for more details.
# 
# 	You should have received a copy of the GNU Lesser General Public License
# 	along with Xinc, write to the Free Software
# 	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
################################################################################

CONFIG=@ETC@/config.xml
LOG=@LOG@/xinc.xml
STATUS=@LOG@/xinc
 
DAEMON=/bin/xinc
NAME=xinc
OPTIONS="-f $CONFIG -l $LOG -s $STATUS"
DESC="Xinc - Continuous Integration for PHP"

# create the /etc/xinc/status directory if not exists
if [ ! -r "$STATUS" ]; then
    mkdir $STATUS
fi

. /lib/lsb/init-functions

case "$1" in
  start)
	log_begin_msg "Starting $DESC"
	start-stop-daemon --start --quiet --exec $DAEMON -- $OPTIONS
	log_end_msg $?
	;;
  stop)
	log_begin_msg "Stopping $DESC"
	start-stop-daemon --stop --quiet --name $NAME
	log_end_msg $?
	;;
  restart|force-reload)
	log_begin_msg "Restarting $DESC"
	if start-stop-daemon --stop --quiet --name $NAME; then
		start-stop-daemon --start --quiet --exec $DAEMON -- $OPTIONS
	fi
	log_end_msg $?
	;;
  status)
	echo -n "Status of $DESC: "
	if [ ! -r "$PIDFILE" ]; then
		echo "$NAME is not running."
		exit 3
	fi
	if read pid < "$PIDFILE" && ps -p "$pid" > /dev/null 2>&1; then
		echo "$NAME is running."
		exit 0
	else
		echo "$NAME is not running but $PIDFILE exists."
		exit 1
	fi
	;;
  *)
	N=/etc/init.d/${0##*/}
	echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
	exit 1
	;;
esac

exit 0
